package com.godGuang.patient.service;

import com.alibaba.fastjson.JSON;
import com.godGuang.common.core.utils.uuid.UUID;
import com.godGuang.common.security.service.TokenService;
import com.godGuang.patient.mapper.ConsumeRecordMapper;
import com.patient.domin.Account;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class ConsumeRecordService {

    @Autowired
    private ConsumeRecordMapper consumeRecordMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private final String ADD_LOG_QUEUE_NAME = "aaa";

    @Transactional
    public void recordConsume(Account account) {
        // 创建消费记录对象并设置相应属性
        Account record = new Account();
        Long userId = tokenService.getLoginUser().getUserid();
        record.setUserId(userId);
        record.setAccountTitle(account.getAccountTitle());
        record.setAccountDefault(account.getAccountDefault());
        record.setAccountTime(account.getAccountTime());
        record.setAccountStatus(account.getAccountStatus());
        rabbitTemplate.convertAndSend(ADD_LOG_QUEUE_NAME, JSON.toJSONString(account),message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return  message;
        });
        // 插入记录到数据库
        consumeRecordMapper.insert(record);
    }
}
